/* Gauge - Performance Measurement Framework for Mobile Platforms
 * Copyright (C) 2013, Juha Hytonen <juha.a.hytonen@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.gauge;

/**
 * Benchmark execution log.
 * 
 * Call this class' static methods to add entries to the log. If no implementation
 * has been registered, nothing is logged. Each benchmark holds its own execution log.
 * 
 * Implement a concrete log by subclassing this class and overriding the addEntry()
 * method. Register your implementation by calling the Register() method. The 
 * implementation is used for all successive logging.
 * 
 * @see LogEntry
 */
public abstract class Log {
	
	private static Log log;
	
	public final static void Register(Log instance) {
		log = instance;
	}
	
	public final static void Close() {
		if (logIsRegistered()) {
			log.close();
			log = null;
		}
	}
	
	/**
	 * You can perform any necessary actions required when the log for
	 * a benchmark is closed (i.e. written on to disk).
	 */
	protected void close() {
	}
	
	public final static void AddInfo(String message) {
		if (logIsRegistered()) {
			log.addEntry(new LogEntry(message));
		}
	}
	
	public final static void AddError(String message) {
		if (logIsRegistered()) {
			log.addEntry(new LogEntry(message, LogEntry.Type.Error));
		}
	}
	
	public final static void AddEntry(LogEntry entry) {
		if (logIsRegistered()) {
			log.addEntry(entry);
		}
	}
	
	/**
	 * You should add concrete entries into the log here.
	 */
	protected abstract void addEntry(LogEntry entry);
	
	private static boolean logIsRegistered() {
		return (log != null);
	}

}
